﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Security;

namespace msBsc.AppLogic.DataAlc.cls
{
    /// <summary>
    /// 手机短信发送
    /// </summary>
    /// Author:蒲崇彬
    /// Creation date:2015-9-17 10:12
    public class ShortMessage
    {

        private static string Account = "dlz65360519";
        private static string Password = "yzz23567828";
        private static string ShortApi = "http://api.sms.cn/mt/";

        /// <summary>
        /// 短信发送
        /// </summary>
        /// <param name="mobile">电话号码 多个用逗号分割13900008888,13900009999</param>
        /// <param name="strContent">发送内容</param>
        /// <returns>
        ///100 　发送成功
        ///101 　验证失败
        ///102 　短信不足
        ///103 　操作失败
        ///104 　非法字符
        ///105 　内容过多
        ///106 　号码过多
        ///107 　频率过快
        ///108 　号码内容空
        ///109 　账号冻结
        ///110 　禁止频繁单条发送
        ///111 　系统暂定发送
        ///112 　号码错误
        ///113 　定时时间格式不对
        ///114 　账号被锁，10分钟后登录
        ///115 　连接失败
        ///116 　禁止接口发送
        ///120 　系统升级
        ///</returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-17 10:20
        public static string SendShortMessage(string mobile, string strContent)
        {
            StringBuilder sbTemp = new StringBuilder();

            //密码进行MD5加密
            string md5Pwd = FormsAuthentication.HashPasswordForStoringInConfigFile(Password + Account, "MD5");
            //POST 传值
            sbTemp.Append("uid=" + Account + "&pwd=" + md5Pwd + "&mobile=" + mobile + "&content=" + strContent);
            byte[] bTemp = System.Text.Encoding.GetEncoding("GBK").GetBytes(sbTemp.ToString());
            String postReturn = DoPostRequest(ShortApi, bTemp);

            //测试返回结果 sms&stat=100&message=????????
            return postReturn;
        }
        //POST方式发送得结果
        /// Author:蒲崇彬
        /// Creation date:2015-9-17 10:20
        private static String DoPostRequest(string url, byte[] bData)
        {
            System.Net.HttpWebRequest hwRequest;
            System.Net.HttpWebResponse hwResponse;

            string strResult = string.Empty;
            try
            {
                hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
                hwRequest.Timeout = 5000;
                hwRequest.Method = "POST";
                hwRequest.ContentType = "application/x-www-form-urlencoded";
                hwRequest.ContentLength = bData.Length;

                System.IO.Stream smWrite = hwRequest.GetRequestStream();
                smWrite.Write(bData, 0, bData.Length);
                smWrite.Close();
            }
            catch (System.Exception err)
            {
                //WriteErrLog(err.ToString());
                return strResult;
            }

            //获取请求数据
            try
            {
                hwResponse = (HttpWebResponse)hwRequest.GetResponse();
                StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.ASCII);
                strResult = srReader.ReadToEnd();
                srReader.Close();
                hwResponse.Close();
            }
            catch (System.Exception err)
            {
                //WriteErrLog(err.ToString());
            }
            return strResult;
        }

        /// <summary>
        /// 错误日志处理
        /// </summary>
        /// <param name="strErr"></param>
        /// Author:蒲崇彬
        /// Creation date:2015-9-17 10:20
        private static void WriteErrLog(string strErr)
        {
            Console.WriteLine(strErr);
            System.Diagnostics.Trace.WriteLine(strErr);
        }

    }
}